<template>
  <div class="app-container bg-gray">
    <el-row class="box-1">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div>
              <el-select v-model="searchTerms.catId" placeholder="所属分类" class="select" clearable @change="getData">
                <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
              <el-select v-model="searchTerms.status" placeholder="状态" class="select" clearable @change="getData">
                <el-option :value="1" label="待签收" />
                <el-option :value="2" label="处理中" />
                <el-option :value="3" label="已解决" />
              </el-select>
              <el-input v-model="searchTerms.searchValue" placeholder="请输入内容" class="search-input" @change="getData"/>
              <el-button type="success" class="bg-green" icon="el-icon-search" @click="getData">搜索</el-button>
            </div>
            <div>
              <!--<el-button type="primary" class="bg-green" icon="el-icon-download" @click="toExportData">导出数据</el-button>-->
              <el-button type="primary" class="bg-green" icon="el-icon-download" @click="toExportDataDetail">全部导出</el-button>
            </div>
          </div>
          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="50" header-align="center" align="center" fixed />
            <el-table-column prop="id" label="ID" width="65" header-align="center" align="center" sortable />
            <el-table-column prop="userName" label="提交人" min-width="120" header-align="center" align="center"/>
            <el-table-column prop="userMobile" label="联系方式" min-width="160" header-align="center" align="center"/>
            <el-table-column prop="catName" label="所属分类" min-width="140" header-align="center" align="center"/>
            <el-table-column prop="typeName" label="问题类别" min-width="120" header-align="center" align="center">
              <template slot-scope="scope">
                <el-tag type="info">{{ scope.row['typeName'] }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="提交时间" min-width="160" header-align="center" align="center" sortable/>
            <el-table-column prop="status" label="状态" min-width="120" header-align="center" align="center">
              <template slot-scope="scope">
                <el-tag v-if="scope.row['status']==='1'" type="danger">待签收</el-tag>
                <el-tag v-else-if="scope.row['status']==='2'" type="success">处理中</el-tag>
                <el-tag v-else type="info">已结单</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="adminName" label="接单人员" min-width="120" header-align="center" align="center">
              <template slot-scope="scope">{{ scope.row['adminName'] || '--' }}</template>
            </el-table-column>
            <el-table-column prop="adminMobile" label="联系方式" min-width="160" header-align="center" align="center">
              <template slot-scope="scope">{{ scope.row['adminMobile'] || '--' }}</template>
            </el-table-column>
            <el-table-column prop="receiveTime" label="接受时间" min-width="160" header-align="center" align="center" sortable>
              <template slot-scope="scope">{{ scope.row['receiveTime'] || '--' }}</template>
            </el-table-column>
            <el-table-column prop="updateTime" label="反馈时间" min-width="160" header-align="center" align="center" sortable>
              <template slot-scope="scope">{{ scope.row['updateTime'] || '--' }}</template>
            </el-table-column>
            <el-table-column label="操作" width="300" fixed="right">
              <template slot-scope="scope">
                <el-button type="success" size="small" icon="el-icon-search" class="bg-green" @click="toDetails(scope.row)">详情</el-button>
                <el-button v-if="scope.row['status']!=='3'" type="primary" size="small" icon="el-icon-s-check" @click="toHandle(scope.row)">处理</el-button>
                <el-button type="danger" size="small" icon="el-icon-delete" @click="toDelete(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, parseTime } from '@/utils/table';
export default {
  data() {
    return {
      searchTerms: {
        catId: '',
        status: '',
        searchValue: ''
      },

      typeList: [],

      // 表格相关数据
      downloadLoading: false,
      tableMaxHeight: document.documentElement.clientHeight - 280,
      multipleSelection: [],
      // 表格相关参数
      tableLoading: false,
      currentPage: 1,
      pageSize: 10,
      pageSizes: [5, 10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  watch: {},
  created() {
    this.getData();
    this.getTypeList();
  },

  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, parseTime,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'newWork/list/getPageList'
      const data = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        search: this.searchTerms.searchValue,
        catId: this.searchTerms.catId,
        status: this.searchTerms.status
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows;
        this.total = res.data.total;
        this.tableLoading = false;
      }).catch((e) => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },
    /**
     *  获取区域列表
     */
    getTypeList() {
      const url = 'newWork/cat/getAll'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.typeList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    // 跳转详情
    toDetails(row){
      this.$router.push({
        path: '/superWorkOrder/feedbackInfo',
        query: {
          id: row.id
        }
      })
    },

    // 跳转处理
    toHandle(row){
      this.$router.push({
        path: '/superWorkOrder/feedbackInfo',
        query: {
          id: row.id
        }
      })
    },

    toDelete(row) {
      this.$confirm('此操作将删除该工单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'newWork/list/del'
        const data = {
          id: row.id
        }
        this.$store.dispatch('GetConnect', { url, data }).then(res => {
          this.$message.success(res.msg)
          this.getData()
        }).catch(e => {
          this.$message.error(e.msg)
        })
      })
    },

    // tabel選擇
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },

    // 导出数据
    toExportData() {
      if (this.multipleSelection.length === 0) {
        this.$message.error('请选择要导出的工单');
      } else {
        this.downloadLoading = true;
        let list = [];
        this.multipleSelection.forEach(item => {
          list.push(item.id);
        })
        list = list.join(',');
        window.open(this.$store.getters.serverUrl + '/admin/newWork/list/exportData?token=' + this.$store.getters.token + '&id=' + list + '&mallId=' + this.$store.getters.SaaSId);
        this.downloadLoading = false;
      }
    },
    // 导出数据
    toExportDataDetail() {
      this.downloadLoading = true;
        window.open(this.$store.getters.serverUrl + '/admin/newWork/list/exportInfoData?token=' + this.$store.getters.token + '&mallId=' + this.$store.getters.SaaSId);
        this.downloadLoading = false;
      }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../../../styles/index.scss';
  .box-1 {
    .header {
      @include flex-row(center, space-between);
      padding-bottom: 16px;
      .search-input {
        width: 240px;
      }
    }
  }
</style>
